<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>subtract() | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>subtract()</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>subtract()</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/manipulating/subtract/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/03-manipulating/02-subtract.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
</code></pre>

							</div>
<p>&#x901A;&#x8FC7;&#x51CF;&#x53BB;&#x65F6;&#x95F4;&#x6765;&#x6539;&#x53D8;&#x539F;&#x59CB;&#x7684; moment&#x3002;</p>
<p>&#x8FD9;&#x4E0E; <code>moment#add</code> &#x5B8C;&#x5168;&#x76F8;&#x540C;&#xFF0C;&#x53EA;&#x662F;&#x4E0D;&#x589E;&#x52A0;&#x65F6;&#x95F4;&#xFF0C;&#x800C;&#x662F;&#x51CF;&#x53BB;&#x65F6;&#x95F4;&#x3002;</p>
<pre><code class="language-javascript">moment().subtract(7, &apos;days&apos;);
</code></pre>
<p>&#x5728; <strong>2.8.0</strong> &#x7248;&#x672C;&#x4E4B;&#x524D;&#xFF0C;&#x8FD8;&#x652F;&#x6301; <code>moment#subtract(String, Number)</code> &#x8BED;&#x6CD5;&#x3002;
&#x4E0D;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x5B83;&#xFF0C;&#x800C;&#x4F7F;&#x7528; <code>moment#subtract(Number, String)</code>&#x3002;</p>
<pre><code class="language-javascript">moment().subtract(&apos;seconds&apos;, 1); // &#x5E9F;&#x5F03;&#x4E8E; 2.8.0
moment().subtract(1, &apos;seconds&apos;);
</code></pre>
<p>&#x4ECE; <strong>2.12.0</strong> &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;&#x5F53;&#x4E3A;&#x65E5;&#x671F;&#x548C;&#x6708;&#x4EFD;&#x4F20;&#x5165;&#x5C0F;&#x6570;&#x65F6;&#xFF0C;&#x5B83;&#x4EEC;&#x4F1A;&#x88AB;&#x56DB;&#x820D;&#x4E94;&#x5165;&#x5230;&#x6700;&#x63A5;&#x8FD1;&#x7684;&#x6574;&#x6570;&#x3002;
&#x661F;&#x671F;&#x3001;&#x5B63;&#x5EA6;&#x3001;&#x5E74;&#x4EFD;&#x4F1A;&#x88AB;&#x8F6C;&#x6362;&#x5230;&#x65E5;&#x671F;&#x6216;&#x6708;&#x4EFD;&#xFF0C;&#x7136;&#x540E;&#x56DB;&#x820D;&#x4E94;&#x5165;&#x5230;&#x6700;&#x63A5;&#x8FD1;&#x7684;&#x6574;&#x6570;&#x3002;</p>
<pre><code class="language-javascript">moment().subtract(1.5, &apos;months&apos;) == moment().subtract(2, &apos;months&apos;)
moment().subtract(.7, &apos;years&apos;) == moment().subtract(8, &apos;months&apos;) //.7*12 = 8.4&#xFF0C;&#x53D6;&#x6574;&#x5230; 8
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x4E3A;&#x4E86;&#x4F7F;&#x64CD;&#x4F5C; <code>moment.add(-.5, &apos;days&apos;)</code> &#x548C; <code>moment.subtract(.5, &apos;days&apos;)</code> &#x7B49;&#x4EF7;&#xFF0C;-&#x3002;5&#x3001;-1.5&#x3001;-2.5 &#x7B49;&#x90FD;&#x5411;&#x4E0B;&#x820D;&#x5165;&#x3002;</p>
					</div>
          <div id="content_right">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
</code></pre>

							</div>
<p>Mutates the original moment by subtracting time.</p>
<p>This is exactly the same as <code>moment#add</code>, only instead of adding time, it subtracts time.</p>
<pre><code class="language-javascript">moment().subtract(7, &apos;days&apos;);
</code></pre>
<p>Before version <strong>2.8.0</strong>, the <code>moment#subtract(String, Number)</code> syntax was also supported. It has been deprecated in favor of <code>moment#subtract(Number, String)</code>.</p>
<pre><code class="language-javascript">moment().subtract(&apos;seconds&apos;, 1); // Deprecated in 2.8.0
moment().subtract(1, &apos;seconds&apos;);
</code></pre>
<p>As of <strong>2.12.0</strong> when decimal values are passed for days and months, they are rounded to the nearest integer.
Weeks, quarters, and years are converted to days or months, and then rounded to the nearest integer.</p>
<pre><code class="language-javascript">moment().subtract(1.5, &apos;months&apos;) == moment().subtract(2, &apos;months&apos;)
moment().subtract(.7, &apos;years&apos;) == moment().subtract(8, &apos;months&apos;) //.7*12 = 8.4, rounded to 8
</code></pre>
<p>Note that in order to make the operations <code>moment.add(-.5, &apos;days&apos;)</code> and <code>moment.subtract(.5, &apos;days&apos;)</code> equivalent, -.5, -1.5, -2.5, etc are rounded down.</p>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>